package com.yunao.web.controller;

import com.yunao.commons.serachcore.core.bean.Articles;
import com.yunao.commons.serachcore.core.bean.Clusters;
import com.yunao.commons.serachcore.core.bean.Groups;
import com.yunao.commons.serachcore.core.help.GroupField;
import com.yunao.commons.utils.JsonUtil;
import com.yunao.commons.utils.ResultVOUtil;
import com.yunao.commons.vo.ResultVO;
import com.yunao.entity.User;
import com.yunao.service.IsearchService;
import com.yunao.service.PositionService;
import com.yunao.service.UserService;
import com.yunao.web.annotation.UserLoginToken;
import com.yunao.web.utils.TokenUtil;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/search/hot/")
public class HotController {

    @Autowired
    private IsearchService service;

    @Autowired
    private UserService userService;

    @Autowired
    private PositionService positionService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    private static final String SEARCH_HOUR = "1";
    private static final String SEARCH_DAY = "1";
    private static final String SEARCH_WEEK = "1";

    @UserLoginToken
    @RequestMapping("/todayData")
    public ResultVO todayData(){
        Map map2 = new HashMap();
        map2.put("group",GroupField.EMOTION.toString());
        map2.put("days","1");
        Groups groups = service.group(map2);
        return ResultVOUtil.success(groups);
    }

    @UserLoginToken
    @RequestMapping("/mediaData")
    public ResultVO mediaData(){
        Map resultCluster = new HashMap();
        Map map3 = new HashMap();
        map3.put("group",GroupField.MEDIA_TYPE.toString());
        map3.put("days","1");
        Groups groups3 = service.group(map3);
        resultCluster.put("mediatype",groups3);

        Map map4 = new HashMap();
        map4.put("group",GroupField.MEDIA_TYPE.toString());
        map4.put("days","1");
        map4.put("emotion","负面");
        Groups groups4 = service.group(map4);
        resultCluster.put("emotionmediatype",groups4);
        return ResultVOUtil.success(resultCluster);
    }

    @UserLoginToken
    @RequestMapping("/countryhot")
    public ResultVO countryhot(){
        Map map = new HashMap();
        map.put("max_group","11");
        map.put("days","1");
//        map.put("media_type",);
        map.put("message_type","正文");
        map.put("distance","0.5");
        Clusters cluster = service.cluster(map);
        return ResultVOUtil.success(cluster);
    }

    @UserLoginToken
    @RequestMapping("/localhot")
    public ResultVO localhot(HttpServletRequest request){
        User user=TokenUtil.getTokenUser(userService);
        Map map = new HashMap();
        map.put("max_group","10");
        map.put("days","1");
        map.put("message_type","正文");
        map.put("distance","0.5");
        String location = user.getLocaltion();
        if (location.contains(":"))
            location = location.split(":")[0];
        map.put("location",location);
        Clusters cluster = service.cluster(map);
        return ResultVOUtil.success(cluster);
    }

    @UserLoginToken
    @RequestMapping("/indexhot")
    public ResultVO indexhot(HttpServletRequest request){
        User user=TokenUtil.getTokenUser(userService);
        Map resultCluster = new HashMap();
        Map map = new HashMap();
        map.put("max_group","10");
        map.put("days","1");
//        map.put("media_type",);
        map.put("message_type","正文");
        map.put("distance","0.5");
        Clusters cluster = service.cluster(map);
        resultCluster.put("countryhot",cluster);
        String location = user.getLocaltion();
        if (location.contains(":"))
            location = location.split(":")[0];
        map.put("location",location);
        Clusters cluster2 = service.cluster(map);
        resultCluster.put("localhot",cluster2);

        Map map2 = new HashMap();
        map2.put("group",GroupField.EMOTION.toString());
        map2.put("days","1");
        Groups groups = service.group(map2);
        resultCluster.put("emotion",groups);

        Map map3 = new HashMap();
        map3.put("group",GroupField.MEDIA_TYPE.toString());
        map3.put("days","1");
        Groups groups3 = service.group(map3);
        resultCluster.put("mediatype",groups3);

        Map map4 = new HashMap();
        map4.put("group",GroupField.MEDIA_TYPE.toString());
        map4.put("days","1");
        map4.put("emotion","负面");
        Groups groups4 = service.group(map4);
        resultCluster.put("emotionmediatype",groups4);
        return ResultVOUtil.success(resultCluster);
    }


    @UserLoginToken
    @RequestMapping("/location")
    public ResultVO hotByLocation(@RequestParam(value = "days",required = false,defaultValue = "1") String days, String province, String city,
                                  HttpServletRequest request) {
        Map locations=(Map)positionService.finAll().getData();
        Map result = new HashMap();
        Map resultGroup = new HashMap();
        Map resultCluster = new HashMap();
        JSONObject json = new JSONObject(locations);
        Map<String,String> map = new HashMap<>();
        List<String> groupList = new ArrayList<>();
        List<String> clusterList = new ArrayList<>();
        User userOutput=TokenUtil.getTokenUser(userService);
        String location = "";
        String provinceLocal = "";
        if (StringUtils.isEmpty(province)){
            location = userOutput.getLocaltion();
        }
        if (StringUtils.isNotEmpty(province)){
            provinceLocal = province;
            if (city==null){
                location = province;
            }else {
                location = province+":"+city;
            }
        }
        if (location.contains(":")){
            String[] citys = location.split(":");
            provinceLocal = citys[0];
            clusterList.add(provinceLocal);
            clusterList.add(citys[1]);
        }else {
            provinceLocal = location;
            clusterList.add(location);
            for (Object string :json.getJSONArray(location)) {
                clusterList.add(string.toString());
            }
        }
        for (Object string :json.getJSONArray(provinceLocal)) {
            groupList.add(string.toString());
        }
        for (int i = 0; i < groupList.size(); i++) {
            map.put("location",provinceLocal+":"+groupList.get(i));
//            map.put("group",GroupField.LOCATION.toString());
            map.put("days",days);
            Articles articles = service.query(map);
            resultGroup.put(groupList.get(i),articles.getTotal());
        }
        for (int i = 0; i < clusterList.size(); i++) {
            if (i==0)
                map.put("location",provinceLocal);
            else
                map.put("location",provinceLocal+":"+clusterList.get(i));
//            map.put("group",GroupField.LOCATION.toString());
            map.put("max_group","10");
            map.put("days",days);
            map.put("message_type","正文");
            map.put("distance","0.5");
            Clusters cluster = service.cluster(map);
            resultCluster.put(clusterList.get(i),cluster);
        }
        result.put("map",resultGroup);
        result.put("info",resultCluster);
        return ResultVOUtil.success(result);
    }


    @UserLoginToken
    @RequestMapping("/trade")
    public ResultVO hotByTrade(String trade,String days,
                               HttpServletRequest request){
        days = days==null?"1":days;
        Map<String,String> map = new HashMap<>();
        if (StringUtils.isEmpty(trade)){
            User userOutput=TokenUtil.getTokenUser(userService);
            trade = userOutput.getTrade();
        }
        map.put("keywords",trade);
        map.put("max_group","10");
        map.put("days",days);
        map.put("message_type","正文");
        map.put("distance","0.5");
        Clusters clusters = service.cluster(map);
        return ResultVOUtil.success(clusters);
    }

}
